import axios from 'axios'
import * as constans from './constans'
import {fromJS} from 'immutable'

const changeHomeData=(result)=>({
    type:constans.CHANGE_HEOM_DATA,
    topicList: result.topicList,
    articleList: result.articleList,
    RecommerList: result.RecommerList,
    writer: result.writer
})

const addHomeList = (list, page)=>({
    type:constans.ADD_HOME_LIST,
    list:fromJS(list),
    page:page+1
})

export const getHomeData =()=>{
    return (dispatch)=>{
        axios.get('/api/home.json').then((res)=>{
            const result=res.data.data
            const action = changeHomeData(result)
            dispatch(action)
        })
    }
}

export const getMoreList = (page)=>{
    return(dispatch)=>{
    axios.get('./api/homelist.json?page='+page).then((res)=>{
            const result= res.data.data
            const action = addHomeList(result,page)
            dispatch(action)
    }) 
    }
}

export const toggleTopShow = (show)=>({
    type:constans.TOGGLE_SHOW,
    show
})

export const PopoverShow=()=>({
    type:constans.POPOVER_SHOW
})

export const popoverHide =()=>({
    type:constans.POPOVER_HIDE
})